package com.libinyn.base.controller.system;


import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;
import com.libinyn.base.model.sys.RoleEntity;
import com.libinyn.base.service.system.IRoleService;
import com.libinyn.base.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


/**
 * 角色管理
 * @fileName: RoleController.java
 * @author: bin.li01
 * @date: 2017/11/20 15:59
 * @version: v1.0.0
 */
@Controller
@RequestMapping("role")
public class RoleController {

	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
	
	@Autowired
	private IRoleService roleService;
	
	/**
	 * 角色列表
	 */
	@RequestMapping("/list")
	public Result list(RoleEntity roleEntity, PageHelper pageHelper){
		//如果不是超级管理员，则只查询自己创建的角色列表
		List<RoleEntity> roleEntities;
		PageInfo<RoleEntity> pageInfo;
		//查询列表数据
		try {
			PageHelper.startPage(1, 2);
			roleEntities = roleService.select(roleEntity);
			pageInfo = new PageInfo<RoleEntity>(roleEntities);

		} catch (Exception e) {
			logger.error("查询数据失败！",e);
			return Result.error("查询数据失败！");
		}

		return Result.ok().put("pageInfo", pageInfo);
	}
	
	/**
	 * 角色列表
	 */
	@RequestMapping("/select")
	public Result select(RoleEntity roleEntity){

		//如果不是超级管理员，则只查询自己所拥有的角色列表
		List<RoleEntity> roleEntities;
		try {
			roleEntities = roleService.select(roleEntity);
		} catch (Exception e) {
			logger.error("查询数据失败！",e);
			return Result.error("查询数据失败！");
		}

		return Result.ok().put("list", roleEntities);
	}
	
	/**
	 * 角色信息
	 */
	@RequestMapping("/info/{roleId}")
	public Result info(@PathVariable("roleId") Long roleId){
		RoleEntity roleEntity;
		try {
			 roleEntity = roleService.selectByPrimaryKey(roleId);

		} catch (Exception e) {
			logger.error("查询数据失败！",e);
			return Result.error("查询数据失败！");
		}

		return Result.ok().put("role", roleEntity);
	}
	
	/**
	 * 保存角色
	 */
	@RequestMapping("/save")
	public Result save(RoleEntity role){
		Result result = Result.ok("新增角色成功");
		try {
			//校验属性值 ValidatorUtils.validateEntity(role);
			int row = roleService.insert(role);
			if (row < 0){
				logger.error("保存数据失败！{}",role);
				result = Result.error("保存数据失败！");
			}
		} catch (Exception e) {
			logger.error("保存数据失败！",e);
			result = Result.error("保存数据失败！");
		}

		return result;
	}
	
	/**
	 * 修改角色
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody RoleEntity role){

		Result result = Result.ok("修改角色成功");
		try {
			//校验属性值 ValidatorUtils.validateEntity(role);
			int row = roleService.updateByPrimaryKey(role);
			if (row < 0){
				logger.error("修改数据失败！{}",role);
				result = Result.error("修改数据失败！");
			}
		} catch (Exception e) {
			logger.error("修改数据失败！",e);
			result = Result.error("修改数据失败！");
		}
		return result;
	}
	

	@RequestMapping("/delete")
	public Result delete(@RequestBody String roleIds){
		Result result = Result.ok("删除角色成功");
		try {
			//校验属性值 ValidatorUtils.validateEntity(role);
			int row = roleService.deleteByIds(roleIds);
			if (row < 0){
				logger.error("删除数据失败！{}",roleIds);
				result = Result.error("删除数据失败！");
			}
		} catch (Exception e) {
			logger.error("删除数据失败！",e);
			result = Result.error("删除数据失败！");
		}
		return result;
	}
}
